﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLAddress
    {
        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="address">地址信息</param>
        /// <param name="isDefault">是否是默认收货地址.  true:是  false:不是</param>
        /// <returns>如果添加失败你就返回0，成功就返回ID</returns>
        public int AddAddress(int studentId, string address, bool isDefault, string recipient, string phoneNum)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                if (isDefault) //update address,remove default address.
                {
                    RemoveDefalutAddress(studentId);
                }
                var stu = db.Students.Single(s => s.Students_Id == studentId);
                Address ad = new Address
                {
                    Address_Info = address,
                    IsDefault = isDefault ? 1 : 0,
                    recipient = recipient,
                    phoneNum = phoneNum
                };
                db.Addresses.InsertOnSubmit(ad);
                db.SubmitChanges();
                MyAddress myAd = new MyAddress { Address = ad, Student = stu };
                db.MyAddresses.InsertOnSubmit(myAd);
                db.SubmitChanges();
                return ad.Address_Id;
            }
        }

        /// <summary>
        /// 获取学生的所有收货地址
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <returns></returns>
        public List<MAddress> GetStudentAllAddress(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var address = db.MyAddresses.Where(d => d.Student.Students_Id == studentId)
                                .OrderByDescending(d => d.Id)
                                .Select(d => new MAddress
                                {
                                    AddressId = d.Address.Address_Id,
                                    AddressInfo = d.Address.Address_Info,
                                    Recipient = d.Address.recipient ?? "",
                                    PhoneNum = d.Address.phoneNum ?? "",
                                    IsDefault = d.Address.IsDefault == 1
                                })
                                .ToList();
                return address;

            }
        }

        /// <summary>
        /// 获取学生的默认收货地址.
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <returns></returns>
        public MAddress GetDefaultAddress(int studentId)
        {
            return GetStudentAllAddress(studentId).FirstOrDefault(d => d.IsDefault);
        }

        /// <summary>
        /// 修改学生的收货地址信息.
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="addressId">地址ID</param>
        /// <param name="addressInfor">地址信息</param>
        /// <param name="isDefault">是否是默认收货地址.  true:是  false:不是</param>
        /// <returns>true:更新成功  false:更新失败</returns>
        public bool UpdateAddress(int studentId, int addressId, string addressInfor, bool isDefault, string recipient, string phoneNum)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                if (isDefault) //update address,remove default address.
                {
                    RemoveDefalutAddress(studentId);
                }
                var add = db.Addresses.Single(d => d.Address_Id == addressId);
                add.Address_Info = addressInfor;
                add.IsDefault = isDefault ? 1 : 0;
                add.recipient = recipient;
                add.phoneNum = phoneNum;
                db.SubmitChanges();
                return true;
            }
        }

        /// <summary>
        /// 删除收货地址.
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="addressId">地址ID</param>
        /// <returns>true:删除成功  false:删除失败</returns>
        public bool DeleteAddress(int studentId, int addressId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var add = db.MyAddresses.Single(m => m.Student.Students_Id == studentId && m.Address.Address_Id == addressId);
                db.MyAddresses.DeleteOnSubmit(add);
                db.SubmitChanges();
                return true;
            }
        }

        void RemoveDefalutAddress(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {

                var defaultAddress = db.MyAddresses.Where(d => d.Student.Students_Id == studentId && d.Address.IsDefault == 1);
                foreach (var item in defaultAddress)
                    item.Address.IsDefault = 0;
                db.SubmitChanges();
            }
        }
    }
}